package org.bbs.demo.topic.api;

import org.apache.el.parser.Token;
import org.bbs.demo.common.BBSConstants;
import org.bbs.demo.common.Result;
import org.bbs.demo.topic.dao.CancelLikes;
import org.bbs.demo.topic.dao.ImageDao;
import org.bbs.demo.topic.dao.LikesDao;
import org.bbs.demo.topic.dao.PostDao;
import org.bbs.demo.topic.dto.ImageDto;
import org.bbs.demo.topic.dto.LikesDto;
import org.bbs.demo.topic.dto.PostDto;
import org.bbs.demo.utils.jwtutil.JWTutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/post")
@Transactional
public class PostApi {

    @Autowired
    private PostDao postDao;

    @PostMapping("/tips/text")
    public Result postTips(@RequestBody PostDto postDto, @RequestHeader(BBSConstants.HEADER_PARAM_TOKEN) String Token){
        //checkconteng
        if ( !postDto.checkContent()){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"内容不符合要求");
        }
        try {
            String t_id = UUID.randomUUID().toString().replace("-","");
            String user_id = JWTutil.getUserID(Token);
            postDto.setTopic_id(t_id);
            postDto.setUser_id(user_id);

            //插入topic数据
            postDao.topicInsert(postDto);

            for (int i = 0; i < postDto.getTagger().size(); i++){
                postDao.taggerInsert(postDto.getTagger().get(i));
                //插入tagger_use
                postDao.taggerTopicInsert(postDto.getTopic_id(), postDto.getTagger().get(i));
            }

            for (int j =0; j < postDto.getImageList().size(); j++){
                //插入图片
                postDao.imageInsert(postDto.getImageList().get(j), postDto.getTopic_id());
            }
            return Result.OK("发送成功",t_id);

        }catch (DuplicateKeyException err){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"发送失败");
        }
    }

    /**@Autowired
    private ImageDao imageDao;

    @PostMapping("/tips/record")
    public Result recordImage(@RequestBody ImageDto imageDto){
        //System.out.println(imageDto.getImage_id().size());
        try {
            System.out.println(imageDto.getImage_id().size());
            for (int i = imageDto.getImage_id().size() -1 ; i >=0 ; i--){
                //System.out.println(imageDto.getImage_id().get(i));
                imageDao.imageInsert(imageDto.getImage_id().get(i), imageDto.getTopic_id());
                //System.out.println(imageDto.getTopic_id());
            }
            return Result.OK("图片记录成功");
        }catch (DuplicateKeyException err){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"图片丢失");
        }
    }**/

    @PostMapping("/tips/uploadimage")
    public Result uploadImage(MultipartFile file) throws IOException {
        String filename = UUID.randomUUID().toString().replace("-","");
        InputStream in = file.getInputStream();

        OutputStream out = new FileOutputStream(BBSConstants.UPLOAD_DIR + "/" + filename);

        byte[] b = new byte[1024*1024];
        int len = -1;

        while ((len = in.read(b)) != -1){
            out.write(b,0,len);
        }

        out.flush();
        out.close();
        in.close();

        return Result.OK(filename);
    }

    @Autowired
    private LikesDao likesDao;
    @PostMapping("/tips/like")
    public Result topicLike(@RequestBody LikesDto likesDto, @RequestHeader(BBSConstants.HEADER_PARAM_TOKEN) String Token){
        try {
            String user_id = JWTutil.getUserID(Token);
            String topic_id = likesDto.getTopic_id();
            likesDao.logLike(user_id,topic_id);
            System.out.println(topic_id);
            likesDao.updateLike(topic_id);
            likesDao.likesSexy(topic_id);


            return Result.OK("点赞成功");
        }catch (DuplicateKeyException e){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"点赞失败");
        }
    }


    @Autowired
    private CancelLikes cancelLikes;
    @PostMapping("/tips/cancel")
    public Result cancelLikes(@RequestBody LikesDto likesDto, @RequestHeader(BBSConstants.HEADER_PARAM_TOKEN) String Token){
        try{
            String user_id = JWTutil.getUserID(Token);
            String topic_id = likesDto.getTopic_id();
            int i =cancelLikes.cancelLike(user_id, topic_id);
            if (i == 0){
                return Result.err(BBSConstants.CODE_ERR_BUSINESS,"重复取消");
            }
            System.out.println(i);
            cancelLikes.updateCancelLike(topic_id);
            cancelLikes.reduceSexy(topic_id);
            return Result.OK("成功取消点赞");
        }catch (DuplicateKeyException e){
            return Result.err(BBSConstants.CODE_ERR_BUSINESS,"取消失败");
        }
    }

    /*@Autowired*/

}
